<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Media extends CI_Controller {
	public function index() {
		redirect ( "media/media_index" );
	}
	public function media_index() {
		$this->load->model ( 'region_model' );
		$data ['regions'] = $this->region_model->query_region_list ( 0 );
		$this->load->model ( "media_model" );
		$data ['statistics'] = $this->media_model->query_statistics ();
		$data ['common_media_list'] = $this->media_model->query_featured_list ( "common", 7 );
		$data ['led_media_list'] = $this->media_model->query_featured_list ( "led", 8 );
		$data ['highway_media_list'] = $this->media_model->query_featured_list ( "highway", 6 );
		// 媒体公告
		$data ['media_announcement'] = $this->media_model->query_media_announcement ();
		
		$data ['title'] = "广告媒体搜索";
		$this->load->view ( 'media/media_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'media/media' );
		$this->load->view ( 'common/footer' );
	}
	public function media_search() {
		$this->load->helper ( "common" );
		$data ['media_type'] = $this->input->get ( "media_type" );
		$data ['province'] = $this->input->get ( "province" );
		$data ['city'] = $this->input->get ( "city" );
		$data ['area'] = $this->input->get ( "area" );
		$data ['highway'] = $this->input->get ( "highway" );
		$data ['airport'] = $this->input->get ( "airport" );
		$data ['price'] = $this->input->get ( "price" );
		$data ['start_price'] = $this->input->get ( "start_price" );
		$data ['end_price'] = $this->input->get ( "end_price" );
		$data ['media_type_id'] = $this->input->get ( "media_type_id" );
		$data ['env_type'] = $this->input->get ( "env_type" );
		$data ['illumination'] = $this->input->get ( "illumination" );
		$data ['sort_by'] = $this->input->get ( "sort_by" );
		$data ['sort_method'] = $this->input->get ( "sort_method" );
		$data ['show_type'] = $this->input->get ( "show_type" );
		if ($data ['media_type'] == false) {
			$data ['media_type'] = "common";
		}
		if ($data ['price'] === false) {
			$data ['price'] = "";
		}
		if ($data ['illumination'] === false) {
			$data ['illumination'] = "";
		}
		if ($data ['env_type'] === false) {
			$data ['env_type'] = "";
		}
		if ($data ['media_type_id'] === false) {
			$data ['media_type_id'] = "";
		}
		if ($data ['sort_by'] != "specification") {
			$data ['sort_by'] = "reference_price";
		}
		if ($data ['sort_method'] != "asc") {
			$data ['sort_method'] = "desc";
		}
		if ($data ['show_type'] != "media_search_list") {
			$data ['show_type'] = "media_search_grid";
		}
		// 分页大小
		$start = intval ( $this->input->get ( "per_page" ) );
		$limit = intval ( $this->input->get ( "page_size" ) );
		if ($limit < 1) {
			$limit = 20;
		}
		$data ['page_size'] = $limit;
		// 定义搜索条件
		$data ['search_data'] = $data;
		
		// 媒体信息
		$this->load->model ( 'media_model' );
		$data ['media_list'] = $this->media_model->search_media ( $data ['search_data'], $start, $limit );
		$data ['num'] = $this->media_model->search_media_num ( $data ['search_data'] );
		$data ['media_type_list'] = $this->media_model->query_media_type_list ();
		
		// 分页
		$this->load->library ( 'common/my_page' );
		$url = '?' . http_build_query ( $data ['search_data'] );
		$data ['list_top_page_link'] = $this->my_page->get_pagination ( $url, $data ['num'], $limit, false );
		$data ['bottom_page_link'] = $this->my_page->get_pagination ( $url, $data ['num'], $limit );
		
		// 媒体筛选条件
		$data ['selected_media_type_name'] = "";
		if ($data ['media_type_id']) {
			$data ['selected_media_type_name'] = $this->media_model->query_media_type_name ( $data ['media_type_id'] );
		}
		$data ['selected_env_name'] = "";
		if ($data ['env_type']) {
			$data ['selected_env_name'] = get_env_name ( $data ['env_type'] );
		}
		
		// 区域
		$this->load->model ( 'region_model' );
		$data ['regions'] = $this->region_model->query_region_list ( 0 );
		$data ['citys'] = array ();
		if ($data ['province'] != "") {
			$data ['citys'] = $this->region_model->query_region_list ( $data ['province'] );
		}
		$data ['areas'] = array ();
		if ($data ['city'] != "") {
			$data ['areas'] = $this->region_model->query_region_list ( $data ['city'] );
		}
		$data ['highways'] = array ();
		if ($data ['media_type'] == "highway") {
			$data ['highways'] = $this->region_model->get_highways ( $data ['province'] );
		}
		$data ['airports'] = array ();
		if ($data ['media_type'] != "airport") {
			$data ['airports'] = $this->region_model->get_airports ( $data ['province'] );
		}
		
		// 获取已经加入对比的媒体
		$data ['contrast_media_id_list'] = $this->get_contrast_media_id_list ();
		$data ['contrast_media_list'] = $this->media_model->query_contrast_media_list ( $data ['contrast_media_id_list'] );
		
		// 获取已经加入对比的媒体
		$data ['browse_history_id_list'] = $this->get_browse_history_id_list();
		$data ['browse_history_media_list'] = $this->media_model->query_contrast_media_list ( $data ['browse_history_id_list'] );
		
		// 统计数据
		$data ['statistics'] = $this->media_model->query_statistics ();
		
		$data ['title'] = "广告媒体搜索";
		$this->load->view ( 'media/media_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'media/' . $data ['show_type'] );
		$this->load->view ( 'common/footer' );
		$this->load->view ( 'media/media_db' );
	}
	public function add_contrast_media() {
		$media_id = $this->input->get ( "media_id" );
		if ($media_id == - 1) {
			setcookie ( "contrast_media_id", "" );
		} else {
			$contrast_media_id = "";
			if (isset ( $_COOKIE ['contrast_media_id'] )) {
				$contrast_media_id = $_COOKIE ['contrast_media_id'];
			}
			if (! strstr ( $contrast_media_id, $media_id )) {
				$contrast_media_id .= $media_id . ",";
			} else {
				$contrast_media_id = str_replace ( $media_id . ",", "", $contrast_media_id );
			}
			setcookie ( "contrast_media_id", $contrast_media_id );
		}
	}
	public function contrast_status() {
		$contrast_status = $this->input->get ( "status" );
		setcookie ( "contrast_status", $contrast_status );
	}
	public function media_contrast() {
		$this->load->model ( 'media_model' );
		// 获取已经加入对比的媒体
		$data ['contrast_media_id_list'] = $this->get_contrast_media_id_list ();
		$data ['contrast_media_list'] = $this->media_model->query_contrast_media_detail_list ( $data ['contrast_media_id_list'] );
		$data ['title'] = "广告媒体对比";
		$this->load->helper ( "common" );
		$this->load->view ( 'media/media_contrast_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'media/media_contrast' );
		$this->load->view ( 'common/footer' );
	}
	private function get_contrast_media_id_list() {
		$contrast_media_id_list = array ();
		if (isset ( $_COOKIE ['contrast_media_id'] )) {
			$contrast_media_id_list = explode ( ",", substr ( $_COOKIE ['contrast_media_id'], 0, - 1 ) );
		}
		return $contrast_media_id_list;
	}
	public  function l() {
		$contrast_media_id_list = array ();
		if (isset ( $_COOKIE ['contrast_media_id'] )) {
			$contrast_media_id_list = explode ( ",", substr ( $_COOKIE ['contrast_media_id'], 0, - 1 ) );
		}
		print_r($contrast_media_id_list) ;
	}
	public function media_detail() {
		$media_id = $this->input->get ( "id" );
		$this->load->model ( 'media_model' );
		// 获取已经加入对比的媒体
		$data ['media_detail'] = $this->media_model->query_media_detail ( $media_id );
		$data ['media_pic_list'] = $this->media_model->query_media_pic_list ( $media_id );
		$data ['media_monitor'] = $this->media_model->query_media_monitor ( $media_id );
		$data ['vendor_info'] = $this->media_model->query_vendor_info ( $data ['media_detail'] ['vendor'] );
		$data ['nearby_media_list'] = $this->media_model->query_nearby_media ( $data ['media_detail'] ['longitude'], $data ['media_detail'] ['latitude'] );
		$data ['env_info'] = $this->get_media_env ( $data ['media_detail'] ['media_type'], $media_id, $data ['media_detail'] ['highway_id'], $data ['media_detail'] ['airport_id'] );
		$data ['title'] = $data ['media_detail'] ['location'];
		// 增加访问历史
		$this->add_browse_history ();
		
		$this->load->helper ( "common" );
		$this->load->view ( 'media/media_detail_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'media/media_detail' );
		$this->load->view ( 'common/footer' );
	}
	private function get_media_env($media_type, $media_id, $highway_id, $airport_id) {
		$this->load->helper ( "common" );
		$env = array (
				"name" => "",
				"detail" => "",
				"type" => "" 
		);
		$this->load->model ( 'media_model' );
		if ($media_type == "common" || $media_type == "led") {
			$first_env = $this->media_model->query_media_env ( $media_id );
			if ($first_env != null) {
				$env ['type'] = $first_env ['env_type'];
				$env_info = $this->media_model->query_env_urban_env ( $first_env ['env_id'], $first_env ['env_type'] );
				if ($env_info != null) {
					$env ['detail'] = $env_info;
					$env ['name'] = $env_info ['env_name'];
				}
			}
		} elseif ($media_type == "highway") {
			$env ['type'] = "highway";
			$highway_info = $this->media_model->query_highway_info ( $highway_id );
			$env ['name'] = $highway_info ['highway_name'];
			$env ['detail'] = $highway_info;
		} elseif ($media_type == "airport") {
			$env ['type'] = "airport";
			$airport_info = $this->media_model->query_airport_info ( $highway_id );
			$env ['detail'] = $airport_info;
			$env ['name'] = $highway_info ['airport_name'];
		}
		return $env;
	}
	private function add_browse_history() {
		$media_id = $this->input->get ( "id" );
		if ($media_id == - 1) {
			setcookie ( "browse_media_id", "" );
		} else {
			$browse_media_id = "";
			if (isset ( $_COOKIE ['browse_media_id'] )) {
				$browse_media_id = $_COOKIE ['browse_media_id'];
			}
			if (! strstr ( $browse_media_id, $media_id )) {
				$browse_media_id .= $media_id . ",";
			} else {
				$browse_media_id = str_replace ( $media_id . ",", "", $browse_media_id );
				$browse_media_id .= $media_id . ",";
			}
			setcookie ( "browse_media_id", $browse_media_id );
		}
	}
	private function get_browse_history_id_list() {
		$browse_media_id_list = array ();
		if (isset ( $_COOKIE ['browse_media_id'] )) {
			$browse_media_id_list = explode ( ",", substr ( $_COOKIE ['browse_media_id'], 0, - 1 ) );
		}
		return $browse_media_id_list;
	}
}
